package com.yspa.knowledeUser.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.hsnn.medstgmini.model.Img;
import com.hsnn.medstgmini.model.IsStatus;
import com.hsnn.medstgmini.util.ImgUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseDate;
import com.yspa.knowledeUser.dao.KnowledeUserDao;
import com.yspa.knowledeUser.model.form.KnowledeUserForm;
import com.yspa.knowledeUser.service.KnowledeUserManager;

@Service
public class KnowledeUserManagerImpl implements KnowledeUserManager {

	@Autowired
	private KnowledeUserDao dao;
	public Pagination getKnowledeUserList(Pagination page){
		page = dao.getKnowledeUserList(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page.getRows();
		if (null != list && list.size() > 0){
			for (int i = 0; i < list.size(); i++){
				Map<String, Object> map = list.get(i);
				map.put("is_top", map.get("is_top")==null?"":IsStatus.getName(map.get("is_top").toString()));
				map.put("create_time",map.get("create_time")==null?"":ParseDate.parseFullFormat(ParseDate.fromFullFormat(map.get("create_time").toString())));
			}
		}
		return page;
	}
	public void addUser(MultipartFile img, KnowledeUserForm form){
		if (null != img){
			Img up = ImgUtil.uploadFileProfile(img);
			form.setImgPath(up.getPath());
		}
		dao.addUser(form);
	}
	public Map<String, Object> getEditInfo(String id){
		List list = dao.getEditInfo(id);
		if (null != list && list.size() > 0){
			Map<String, Object> map = (Map<String, Object>) list.get(0);
//			if (map.get("is_top").toString().equals("true")){
//				map.put("is_top", 1);
//			} else {
//				map.put("is_top",0);
//			}
			return map;
		}
		return null;
	}
	public void editInfo(KnowledeUserForm form,MultipartFile img){
		if (null != img){
			Img up = ImgUtil.uploadFileProfile(img);
			form.setImgPath(up.getPath());
		}
		dao.editInfo(form);
	}
	public void deleteUser(List<String> list){
		dao.deleteUser(list);
	}
}
